//
//  WelcomeViewController.swift
//  SinaWeiBo
//
//  Created by 有风 on 16/2/19.
//  Copyright © 2016年 YouFeng. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func loadView() {
        view = UIImageView(image: UIImage(named: "ad_background"))
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnim()
    }
    
    func startAnim() {
        let offSet = -UIScreen.mainScreen().bounds.height + 160
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offSet)
        }
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.88, initialSpringVelocity: 8.8, options: [], animations: { () -> Void in
            //必须加
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                    
                        NSNotificationCenter.defaultCenter().postNotificationName(KeyChoseVC, object: "welcome")
                })
        }
}
    
    func setupUI() {

        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-160)
            
            //iconView.layer.
            
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        
        //
        welcomeLabel.alpha = 0
        //设置头像
       iconView.sd_setImageWithURL(AccountViewModel.sharedAccountViewModel.headImageUrl)
        //设置头像圆角
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
    }
    
    
    //懒加载头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
    //欢迎文案
    private lazy var welcomeLabel: UILabel = UILabel(text: (AccountViewModel.sharedAccountViewModel.userName ?? "") + ",欢迎回来", fontSize: 14, textColor: UIColor.darkGrayColor())
}
